package com.guigusuqi.activiti.controller;


import com.guigusuqi.activiti.dto.CompleteTaskDTO;
import com.guigusuqi.activiti.dto.TbInsertEvection;
import com.guigusuqi.activiti.entity.TbEvection;
import com.guigusuqi.activiti.mapper.TbEvectionMapper;
import com.guigusuqi.activiti.mapper.converter.EvectionConvertMapper;
import com.guigusuqi.activiti.service.TbEvectionService;
import com.guigusuqi.commonutils.utils.GetTokenByAuthorizationUtils;
import com.guigusuqi.commonutils.utils.RedisCache;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.user.entity.LoginUser;
import com.guigusuqi.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.history.HistoricTaskInstance;
import org.apache.poi.xwpf.usermodel.TOC;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suqi
 * @since 2023-06-15
 */
@RestController
@RequestMapping("/evection")
@Api(tags = {"流程实例管理"})
public class TbEvectionController
{
    @Resource
    private TbEvectionService tbEvectionService;

    @Resource
    private SysUserService sysUserService;

    @ApiOperation("用户新增出差申请")
    @PostMapping("createEvection")
    public Result<String> createEvection(HttpServletRequest request, @Valid @RequestBody TbInsertEvection insertEvection)
    {
        String processDefinitionId = tbEvectionService.createEvection(insertEvection);
        return Result.success("新增出差申请成功!",processDefinitionId);
    }

    @ApiOperation("获取用户当前的审批流程列表")
    @PostMapping("listEvectionByUserId")
    public Result<List<Map<String, Object>>> listEvectionByUserId()
    {
        List<Map<String, Object>> taskList = tbEvectionService.listEvectionByUserId();
        return Result.success(taskList);
    }

    @ApiOperation("获取用户历史的审批流程列表")
    @PostMapping("listHistoryEvectionByUserId")
    public Result<List<HistoricTaskInstance>> listHistoryEvectionByUserId()
    {
        List<HistoricTaskInstance> historicTaskInstances = tbEvectionService.listHistoryEvectionByUserId();

        return Result.success(historicTaskInstances);
    }


    @ApiOperation("完成审批任务")
    @PostMapping("completeTask")
    public Result<Boolean> completeTask(@Valid @RequestBody CompleteTaskDTO completeTaskDTO)
    {
        String token = GetTokenByAuthorizationUtils.getToken();
        LoginUser loginUser = sysUserService.checkAndParseToken(token);

        tbEvectionService.completeTask(completeTaskDTO.getRemark(), completeTaskDTO.getTaskId(), loginUser.getUser().getUserId());
        return Result.success("完成审批任务!");
    }

}

